package com.smarthome.tools;

import android.R.integer;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.Shader;
import android.graphics.PorterDuff.Mode;
import android.graphics.Shader.TileMode;

public final class ImageHandler {
	private Bitmap sourceBitmap;
	public ImageHandler(Bitmap sourceBitmap) {
		// TODO Auto-generated constructor stub
		this.sourceBitmap=sourceBitmap;
	}
	public void setBitmap(Bitmap bitmap){
		sourceBitmap=bitmap;
	}
	public Bitmap getBitmap(){
		return sourceBitmap;
	}
	public int getWidth(){
		return sourceBitmap.getWidth();
	}
	public int getHeight(){
		return sourceBitmap.getHeight();
	}
	public Bitmap getReflectionBitmap(){
		int width=getWidth();
		int height=getHeight();
		Matrix matrix = new Matrix();
		matrix.preRotate(180);
		Bitmap reflectionBitmap=Bitmap.createBitmap(sourceBitmap, 0, 0, 
				width, height, matrix, false);
		
		return reflectionBitmap;	
	}
	public static Bitmap getReflectionBitmap(Bitmap originalBitmap){
		int width=originalBitmap.getWidth();
		int height=originalBitmap.getHeight();
		Matrix matrix = new Matrix();
		matrix.preRotate(180);
		Bitmap reflectionBitmap=Bitmap.createBitmap(originalBitmap, 0, 0, 
				width, height, matrix, false);
		
		return reflectionBitmap;
	}
		
	public Bitmap getScaledBitmap(float newWidth,float newHeight){
		float width=getWidth();
		float height=getHeight();
		float xScale=newWidth/width;
		float yScale=newHeight/height;
		Matrix matrix = new Matrix();
		matrix.postScale(xScale, yScale);
		Bitmap spicifiedSizeBitmap=Bitmap.createBitmap(sourceBitmap, 0, 0, 
				(int)width, (int)height, matrix, false);
			
		return spicifiedSizeBitmap;
	}
	public static Bitmap getScaledBitmap(Bitmap originalBitmap,float newWidth,float newHeight){
		float width=originalBitmap.getWidth();
		float height=originalBitmap.getHeight();
		float xScale=newWidth/width;
		float yScale=newHeight/height;
		Matrix matrix = new Matrix();
		matrix.postScale(xScale, yScale);
		Bitmap spicifiedSizeBitmap=Bitmap.createBitmap(originalBitmap, 0, 0, 
				(int)width, (int)height, matrix, false);
			
		return spicifiedSizeBitmap;
	}
		
	public Bitmap getBitmapWithLinearGradient(int top,int left,int right,int reflectionGap){
		int width=getWidth();
		int height=getHeight();
			
		Bitmap bitmapWithLinearGradient = Bitmap.createBitmap(left+width+right,
				2*height, Bitmap.Config.ARGB_8888);
		Bitmap reflectionBitmap = getReflectionBitmap(sourceBitmap);
		Bitmap scaledReflectionBitmap = getScaledBitmap(reflectionBitmap,width, (height)*3/4);
		Canvas canvas = new Canvas(bitmapWithLinearGradient);
		canvas.drawBitmap(sourceBitmap, left, top, null);
			
		Paint paint = createLinearGradient(left, height+top +reflectionGap , width,
				bitmapWithLinearGradient.getHeight()+reflectionGap , 
				0x80ffffff, 0x00ffffff, TileMode.CLAMP);
			
		canvas.drawBitmap(scaledReflectionBitmap, left, height+ top +reflectionGap, null);
		canvas.drawRect(left, height+top+reflectionGap, width, bitmapWithLinearGradient.getHeight()
				+ reflectionGap, paint);
			
		return bitmapWithLinearGradient;
	}
		
	public static Paint createLinearGradient(float x0, float y0, float x1, 
			float y1, int color0, int color1, TileMode tile){
		Paint paint = new Paint();
		LinearGradient lGradient= new LinearGradient(x0, y0, x1, y1, color0, color1, tile);
		paint.setShader(lGradient);
		paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
			
			return paint;
	}
}
